package com.gxy.learn.securityauth.common.exception;

import com.gxy.learn.securityauth.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.CredentialsExpiredException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    private static final String url = "url";
    private static final String errorMsg = "errorMsg";


    /**
     * 捕捉404异常
     *
     * @return
     */
    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ExceptionHandler(NoHandlerFoundException.class)
    public Result handle(HttpServletRequest req, NoHandlerFoundException ex) {
        log.error("{}:{},{}:{}", url, req.getRequestURI(), errorMsg, ex.getMessage(), ex);
        return Result.error("未知的请求！");
    }

    /**
     * 捕捉其他所有异常
     *
     * @param req
     * @param ex
     * @return
     */
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(Exception.class)
    public Result globalException(HttpServletRequest req, Throwable ex) {
        log.error("{}:{},{}:{}", url, req.getRequestURI(), errorMsg, ex.getMessage(), ex);
        return Result.error("请求出现异常！");
    }

    /**
     * 运行时异常
     *
     * @param req
     * @param ex
     * @return
     */
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(BusinessException.class)
    public Result globalException(HttpServletRequest req, BusinessException ex) {
        log.error("{}:{},{}:{}", url, req.getRequestURI(), errorMsg, ex.getMessage(), ex);
        return Result.error(ex.getMessage());
    }


    /**
     * Authorization错误，请重新登录
     *
     * @param req
     * @param ex
     * @return
     */
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ExceptionHandler(BadCredentialsException.class)
    public Result handle(HttpServletRequest req, BadCredentialsException ex) {
        log.error("{}:{},{}:{}", url, req.getRequestURI(), errorMsg, ex.getMessage(), ex);
        return Result.error(String.format("Authorization错误，请重新登录"));
    }

    /**
     * Authorization过期，请重新登录！
     *
     * @param req
     * @param ex
     * @return
     */
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ExceptionHandler(CredentialsExpiredException.class)
    public Result handle(HttpServletRequest req, CredentialsExpiredException ex) {
        log.error("{}:{},{}:{}", url, req.getRequestURI(), errorMsg, ex.getMessage(), ex);
        return Result.error("Authorization过期，请重新登录！");
    }

    /**
     * 用户名不存在 ，请重新登录！
     *
     * @param req
     * @param ex
     * @return
     */
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ExceptionHandler(UsernameNotFoundException.class)
    public Result handle(HttpServletRequest req, UsernameNotFoundException ex) {
        log.error("{}:{},{}:{}", url, req.getRequestURI(), errorMsg, ex.getMessage(), ex);
        return Result.error("Authorization错误，请重新登录！");
    }

}
